//
//  PushShieldFriendViewController.m
//  BGM
//
//  Created by 我要变好看 on 2019/1/24.
//  Copyright © 2019 com.alirtc.demo. All rights reserved.
//

#import "PushShieldFriendViewController.h"
#import "BGMuserFriendClass.h"
#import "ContactDataHelper.h"
#import "ContactSelectTableViewCell.h"

#import "Common.h"
#import "BGMHTTP.h"
#import "BGMUser.h"
#import "SVProgressHUD.h"

@interface PushShieldFriendViewController ()
<
UITableViewDelegate,UITableViewDataSource,
ContactSelectTableViewCellDelegate>

@property (nonatomic, strong)UITableView *userTable;
@property (nonatomic, strong)NSMutableArray *seleArray;

@property (nonatomic, strong)NSArray *sectionArray;  //通讯录
@property (nonatomic, strong)NSArray *rowArray; //通讯录


@end

@implementation PushShieldFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    [self.titleBtn setTitle:self.vcTitle forState:UIControlStateNormal];
    CGRect rect = self.titleBtn.frame;
    rect.size.width = rect.size.width+70;
    self.titleBtn.frame = rect;
    self.backBtn.hidden = NO;
    
    self.navRightBtn.hidden = NO;
    [self.navRightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.navRightBtn setTitleColor:bgmmainColor forState:UIControlStateNormal];
//    @""
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *appFriend = [userDefaults objectForKey:@"appFriend"];
    NSMutableArray *resultArray = [[NSMutableArray alloc]init];
    for (NSDictionary *dic in appFriend) {
        BGMuserFriendClass *friend = [[BGMuserFriendClass alloc]init];
        friend.creatTime = [NSString stringWithFormat:@"%@",[dic objectForKey:@"creatTime"]];
        friend.headImage = [NSString stringWithFormat:@"%@",[dic objectForKey:@"headImage"]];
        friend.friendId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]]; //唯一
        friend.name = [NSString stringWithFormat:@"%@",[dic objectForKey:@"name"]];
        friend.spareOne = [NSString stringWithFormat:@"%@",[dic objectForKey:@"spareOne"]];
        friend.spareTwo = [NSString stringWithFormat:@"%@",[dic objectForKey:@"spareTwo"]];
        friend.type = [NSString stringWithFormat:@"%@",[dic objectForKey:@"type"]];
        friend.userId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userId"]];
        [resultArray addObject:friend];
    }
    
 
    self.rowArray=[ContactDataHelper getFriendListDataBy:resultArray];
    self.sectionArray=[ContactDataHelper getFriendListSectionBy:[self.rowArray mutableCopy]];
    
    self.seleArray = [[NSMutableArray alloc]init];
    for (NSArray *array in self.rowArray) {
        NSMutableArray *newArray = [[NSMutableArray alloc]init];
        for (BGMuserFriendClass *user in array) {
            [newArray addObject:@"0"];
        }
        [self.seleArray addObject:newArray];
    }
    
    
    
    self.userTable = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navview.frame.size.height, SCREENW, SCREENH-self.navview.frame.size.height)];
    self.userTable.delegate = self;
    self.userTable.dataSource = self;
    self.userTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.userTable];
    
    [self.userTable reloadData];
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sectionArray.count;  /////
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        NSArray *array = [self.rowArray objectAtIndex:section];
        return array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 45;

}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
 
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 45)];
        label.text = [NSString stringWithFormat:@"  %@",[self.sectionArray objectAtIndex:section]];
        label.textColor = bgmgrayColor;
        label.font = [UIFont systemFontOfSize:13];
        return label;
 
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section == 0 ? 61 : 52;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"ContactSelectTableViewCell";
    ContactSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ContactSelectTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.seleDelegate = self;
    }
    
    NSArray *array = [self.rowArray objectAtIndex:indexPath.section];
    BGMuserFriendClass *friend = [array objectAtIndex:indexPath.row];
    NSArray *seleArr = [self.seleArray objectAtIndex:indexPath.section];
    NSString *isSele = [seleArr objectAtIndex:indexPath.row];
    [cell setFriendValue:friend indexPath:indexPath isSele:isSele];
    
    return cell;
    
}
//右边
- (NSArray *)sectionIndexTitlesForTableView:(UITableView* )tableView{
    return self.sectionArray;
}

- (void)seleFriend:(NSIndexPath *)indexPath isSele:(BOOL)isSele{
//    NSLog(@"section,row,sele=%d,%d %d",(int)indexPath.section,(int)indexPath.row,isSele );
    NSMutableArray *sectionArray = [self.seleArray objectAtIndex:indexPath.section];
//    NSString *row = [sectionArray objectAtIndex:indexPath.row];
    if(isSele){
        [sectionArray replaceObjectAtIndex:indexPath.row withObject:@"1"];
    }else{
        [sectionArray replaceObjectAtIndex:indexPath.row withObject:@"0"];

    }
    NSLog(@"");
    
}
- (void)clickBack{
    if (self.isPush) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)clickRight{
    NSMutableString *userStr = [[NSMutableString alloc]init];
    NSMutableString *NameStr = [[NSMutableString alloc]init];

    for (int i = 0; i<self.seleArray.count; i++) {
        NSArray *rowArry = [self.seleArray objectAtIndex:i];
        NSArray *friendArray = [self.rowArray objectAtIndex:i];
        for (int j = 0; j<rowArry.count; j++) {
            BGMuserFriendClass *friend = [friendArray objectAtIndex:j];
            NSString *sele = [rowArry objectAtIndex:j];
            if ([sele isEqualToString:@"1"]) {
                [userStr appendString:[NSString stringWithFormat:@"%@,",friend.userId]];
                [NameStr appendString:[NSString stringWithFormat:@"%@,",friend.name]];
            }
        }
    }
    if (self.isPush) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"newSeeFriend" object:NameStr];
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    
    
    NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,@"userId":[BGMUser shareInstance].userId,@"fillterUserIds":userStr};
    
    [BGMHTTP requestWithMethodName:@"users/fillterUsers" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
        NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
        if ([code isEqualToString:@"200"]) {
            [SVProgressHUD showSuccessWithStatus:@"设置成功~"];
            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSNotificationCenter defaultCenter]postNotificationName:@"setfillterUsers" object:NameStr];
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    }];
    
    
}
@end
